﻿using System.Collections.Generic;
using System.Reflection;
using Ninject;

namespace NoMvvm.Ninject
{
    /// <summary>
    /// A set of extensions for setting up Ninject with NoMvvm
    /// </summary>
    public static class Extensions
    {
        internal static IEnumerable<TElement> Flatten<TElement>(this IEnumerable<IEnumerable<TElement>> sequences)
        {
            foreach (var sequence in sequences)
            {
                foreach (var element in sequence)
                {
                    yield return element;
                }
            }
        }
        
#if !SILVERLIGHT
        /// <summary>
        /// Sets up Ninject with default assembly (WPF Only)
        /// </summary>
        /// <param name="runtime"></param>
        /// <returns></returns>
        public static Runtime SetupNinject(this Runtime runtime)
        {
            return runtime.SetupContainer(new Adapter(new StandardKernel()));
        }

        /// <summary>
        /// Sets up Ninject with specified IKernel (WPF Only)
        /// </summary>
        /// <param name="runtime"></param>
        /// <param name="kernel"></param>
        /// <returns></returns>
        public static Runtime SetupNinject(this Runtime runtime, IKernel kernel)
        {
            return runtime.SetupContainer(new Adapter(kernel));
        }
#endif

        /// <summary>
        /// Sets up Ninject with specified assemblies
        /// </summary>
        /// <param name="runtime"></param>
        /// <param name="assemblies"></param>
        /// <returns></returns>
        public static Runtime SetupNinject(this Runtime runtime, params Assembly[] assemblies)
        {
            return runtime.SetupContainer(new Adapter(new StandardKernel(), assemblies));
        }

        /// <summary>
        /// Sets up Ninject with specified IKernel and assemblies
        /// </summary>
        /// <param name="runtime"></param>
        /// <param name="kernel"></param>
        /// <param name="assemblies"></param>
        /// <returns></returns>
        public static Runtime SetupNinject(this Runtime runtime, IKernel kernel, params Assembly[] assemblies)
        {
            return runtime.SetupContainer(new Adapter(kernel, assemblies));
        }
    }
}
